﻿using System;
using System.Linq;
using System.Data;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;


namespace Srvtools
{
    public class TextBoxRemind : TextBox, ISupportInitialize
    {
        private string[] array = null;
        string m_FileName = "";
        public void BeginInit()
        {

        }
        public void EndInit()
        {
            if (!this.DesignMode)
            {
                string pathName = Application.StartupPath + "\\" + Srvtools.CliUtils.fCurrentProject;
                m_FileName = pathName + "\\" + this.FindForm().GetType().Namespace + "." + this.FindForm().Name + "." + this.Name + ".txt";
            }
        }
        public void InitAutoCompleteCustomSource(TextBox textBox)
        {
             
                array = ReadTxt();
                if (array != null && array.Length > 0)
                {
                    AutoCompleteStringCollection ACSC = new AutoCompleteStringCollection();

                    for (int i = 0; i < array.Length; i++)
                    {
                        ACSC.Add(array[i]);
                    }

                    textBox.AutoCompleteCustomSource = ACSC;
                }
           
        }

        string[] ReadTxt()
        {
            try
            {
               

                if (!File.Exists(m_FileName))
                    File.Create(m_FileName);

                return File.ReadAllLines(m_FileName, Encoding.Default);
            }
            catch
            {
                return null;
            }
        }

        public void Remind(string str)
        {
            StreamWriter writer = null;
            try
            {
               // string pathName = Application.StartupPath + "\\" + Srvtools.CliUtils.fCurrentProject;
               // string m_FileName = pathName + "\\" + this.FindForm().GetType().Namespace + "." + this.FindForm().Name + "." + this.Name + ".txt";
                if (array != null && !array.Contains(str))
                {
                    writer = new StreamWriter(m_FileName, true, Encoding.Default);
                    writer.WriteLine(str);
                }
            }
            finally
            {
                if (writer != null)
                {
                    writer.Close();
                    writer = null;
                }
            }
        }
    }
}
